package whu.cn.whushare.util;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

//import com.sun.crypto.provider.SunJCE;

public class EncrypAES {

	// KeyGenerator �ṩ�Գ���Կ������Ĺ��ܣ�֧�ָ����㷨
	private KeyGenerator keygen;
	// SecretKey ���𱣴�Գ���Կ
	private SecretKey deskey;
	// Cipher������ɼ��ܻ���ܹ���
	private Cipher c;
	// ���ֽ����鸺�𱣴���ܵĽ��
	private byte[] cipherByte;
	String key = "cccccccccccccccccccccccccccccccc";

	public static EncrypAES aes = new EncrypAES();

	public EncrypAES() {
//		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		// ʵ��֧��DES�㷨����Կ�����(�㷨��������谴�涨�������׳��쳣)
		try {
			keygen = KeyGenerator.getInstance("AES");
			// �����Կ
			deskey = new SecretKeySpec(asBin(key), "AES");
			;
			// ���Cipher����,ָ����֧�ֵ�DES�㷨
			c = Cipher.getInstance("AES");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static EncrypAES getInstance() {
		return aes;
	}

	/**
	 * ���ַ����
	 * 
	 * @param str
	 * @return
	 * @throws java.security.InvalidKeyException
	 * @throws javax.crypto.IllegalBlockSizeException
	 * @throws javax.crypto.BadPaddingException
	 */
	public byte[] Encrytor(String str) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		// �����Կ����Cipher������г�ʼ����ENCRYPT_MODE��ʾ����ģʽ
		c.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] src = str.getBytes();
		// ���ܣ������cipherByte
		cipherByte = c.doFinal(src);
		return cipherByte;
	}

	public String EncrytorToString(String str) {
		// �����Կ����Cipher������г�ʼ����ENCRYPT_MODE��ʾ����ģʽ
		try {
			c.init(Cipher.ENCRYPT_MODE, deskey);
			byte[] src = str.getBytes("UTF8");
			// ���ܣ������cipherByte
			cipherByte = c.doFinal(src);
			return  parseByte2HexStr(cipherByte);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return str;
		}

	}

	/**
	 * ���ַ����
	 * 
	 * @param buff
	 * @return
	 * @throws java.security.InvalidKeyException
	 * @throws javax.crypto.IllegalBlockSizeException
	 * @throws javax.crypto.BadPaddingException
	 */
	public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		// �����Կ����Cipher������г�ʼ����DECRYPT_MODE��ʾ����ģʽ
		c.init(Cipher.DECRYPT_MODE, deskey);
		cipherByte = c.doFinal(buff);
		return cipherByte;
	}

	public String DecryptorToString(String buff) {
		// �����Կ����Cipher������г�ʼ����DECRYPT_MODE��ʾ����ģʽ
		try {
			c.init(Cipher.DECRYPT_MODE, deskey);
			cipherByte = c.doFinal(parseHexStr2Byte(buff));
			return new String(cipherByte,"UTF8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return buff;
		}

	}

	// ת��Ϊ16����
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * ��16����ת��Ϊ������
	 * 
	 * @param hexStr
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
					16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	/*
	 * תΪ������
	 */
	@SuppressWarnings("unused")
	private static byte[] asBin(String src) {
		if (src.length() < 1)
			return null;
		byte[] encrypted = new byte[src.length() / 2];
		for (int i = 0; i < src.length() / 2; i++) {
			int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
			encrypted[i] = (byte) (high * 16 + low);
		}
		return encrypted;
	}

	/**
	 * @param args
	 * @throws javax.crypto.NoSuchPaddingException
	 * @throws java.security.NoSuchAlgorithmException
	 * @throws javax.crypto.BadPaddingException
	 * @throws javax.crypto.IllegalBlockSizeException
	 * @throws java.security.InvalidKeyException
	 */
	public static void main(String[] args) throws Exception {
		EncrypAES de1 = new EncrypAES();
		String msg = "123";
		String encontent = de1.EncrytorToString(msg);
		String decontent = de1.DecryptorToString(encontent);
		System.out.println("������:" + msg);
		System.out.println("���ܺ�:" + new String(encontent));
		System.out.println("���ܺ�:" + new String(decontent));
	}

}
